package com.itstudy.elasticsearch.utils;

import com.itstudy.elasticsearch.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNameParseUtil
 * @Description TODO
 * @Author miaohuaixiang
 * @Date 2020/6/28 13:46
 * @Version 1.0
 **/
public class ParseUtil {
    public static void main(String[] args) throws IOException {
        new ParseUtil().parseDj("java").forEach(System.out::println);
    }
    public List<Content> parseDj(String keywords) throws IOException {
        String url = "https://search.jd.com/Search?keyword=" + keywords;
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
        ArrayList<Content> goodsList = new ArrayList<>();
        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();
           Content content=new Content();
           content.setTitle(title);
           content.setImg(img);
           content.setPrice(price);
           goodsList.add(content);
        }
return  goodsList;

}






}
